package com.ruoyi.system.monitortask.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.monitortask.domain.MonitorTaskRecord;
import com.ruoyi.system.monitortask.domain.MonitoringErr;
import com.ruoyi.system.monitortask.mapper.MonitoringErrMapper;
import com.ruoyi.system.monitortask.service.IMonitorTaskRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 账号监测查询记录表(monitor_task_record)表控制层
 *
 * @author zyy
 * @date 2024-06-22 14:48:25
 */
@Slf4j
@RestController
@RequestMapping("/monitorTaskRecord")
public class MonitorTaskRecordController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private IMonitorTaskRecordService monitorTaskRecordService;
    @Autowired
    private MonitoringErrMapper monitoringErrMapper;

    /**
     * 分页查询所有数据
     *
     * @param monitorTaskRecord 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public TableDataInfo list(MonitorTaskRecord monitorTaskRecord) {
        Long userId = SecurityUtils.getUserId();
        if (!Objects.equals(userId, 1l)) {
            monitorTaskRecord.setCreateId(userId);
        }
        startPage();

        List<MonitorTaskRecord> list = monitorTaskRecordService.selectList(monitorTaskRecord);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(monitorTaskRecordService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param monitorTaskRecord 实体对象
     * @return 新增结果
     */
    @Log(title = "MonitorTaskRecord", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult insert(@RequestBody MonitorTaskRecord monitorTaskRecord) {
        return toAjax(monitorTaskRecordService.save(monitorTaskRecord));
    }

    /**
     * 修改数据
     *
     * @param monitorTaskRecord 实体对象
     * @return 修改结果
     */
    @Log(title = "MonitorTaskRecord", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody MonitorTaskRecord monitorTaskRecord) {
        return toAjax(monitorTaskRecordService.updateById(monitorTaskRecord));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @Log(title = "MonitorTaskRecord", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(monitorTaskRecordService.removeByIds(Arrays.asList(ids)));
    }

    @PostMapping("/callBack")
    public void callBack(@RequestParam Map<String, Object> map) {
//        {key=147258369, {"type":"check","checkId":1601157,"qq":"135245138","telephone":"18530912310","status":"0",
//        "queryResult":"185******10"}=}
//        log.info("账号监测找回进入了回调");
//        System.out.println(map);
        String backStr = "";
        String keyValue = "";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (StringUtils.equals(entry.getKey(), "key")) {
                keyValue = String.valueOf(entry.getValue());
            } else {
                backStr = entry.getKey();
            }
        }

        if (StringUtils.isEmpty(backStr)) {
            MonitoringErr monitoringErr = new MonitoringErr();
            monitoringErr.setErrText("账号监测获取字符串为空,请管理员检查" + map);
            monitoringErrMapper.insert(monitoringErr);
            return;
        }

        monitorTaskRecordService.callBack(backStr);
    }

}

